Add durable cancellation mode for useAgentChat#1484
Merged
Conversation
🦋 Changeset detectedLatest commit: df0326b The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
agents
@cloudflare/ai-chat
@cloudflare/codemode
hono-agents
@cloudflare/shell
@cloudflare/think
@cloudflare/voice
@cloudflare/worker-bundler
commit: |
|
@whoiskatrin LGTM thank you! |
Contributor
|
Very nice. I like this PR, but I think it could go further. This new toggle should probably be the default behaviour, since it's the overwhelming actual desired usecase (navigating to a new chat session shouldn't kill an existing stream), let's also rename the flag to |
d91facd to
26d433b
Compare
Co-authored-by: Cursor <cursoragent@cursor.com>
26d433b to
d4f226a
Compare
Co-authored-by: Cursor <cursoragent@cursor.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Changes
useAgentChatcancellation semantics so browser/client stream cleanup is local-only by default:By default, the browser is treated as a reconnectable observer of a server turn. Generic client-side stream abort/cleanup does not send
cf_agent_chat_request_cancel, so the server turn can continue and be resumed later. Explicitstop()still sendscf_agent_chat_request_canceland cancels the server turn.For apps that intentionally want request-lifetime behavior, this adds:
Set
cancelOnClientAbort: trueto make generic client abort/stream cleanup cancel the server turn.What the issue actually was
This is not about a raw WebSocket close directly calling server-side abort.
The problematic path is one layer higher:
useAgentChatwraps AI SDKuseChat.useChatowns a local active response with anAbortController.WebSocketChatTransport.sendMessages()receives that signal asoptions.abortSignal, and also exposes aReadableStreamwhosecancel()method can be called by the client stream lifecycle.onAbort()handler.cf_agent_chat_request_cancel.So the server could not distinguish these two cases:
For Durable Object backed agents, the second case should often be recoverable via stream resumption. The browser can disappear or detach while the durable server turn continues and buffers chunks for a later reconnect.
What changed
cancelOnClientAbort?: booleantouseAgentChatandWebSocketChatTransport.false: generic client abort/stream cancel is local-only.trueopts into request-lifetime behavior where generic client abort cancels the server turn.durable/serverTurnCancellationAPI shape in favor of the clearer boolean toggle.useAgentChat().stop()as server-side cancellation regardless ofcancelOnClientAbort.Follow-up changes from review
WebSocketChatTransport.reconnectToStream().observeServerTurn().donefor a detached turn.sendMessages()is called with an already-aborted signal.cancelOnClientAbort: truewith both abort signals andstream.cancel().Tests
npm run test:workers -w @cloudflare/ai-chat -- ws-transport-abort.test.ts ws-transport-cancellation-policy.test.ts ws-transport-resume.test.tsnpm run test:react -w @cloudflare/ai-chat -- use-agent-chat.test.tsxnpm run checkRelated
Fixes #1471